
using System;
using System.Text;
using NuevoGenNHibernate.CEN.Ebay;
using NHibernate;
using NHibernate.Cfg;
using NHibernate.Criterion;
using NHibernate.Exceptions;
using NuevoGenNHibernate.EN.Ebay;
using NuevoGenNHibernate.Exceptions;

namespace NuevoGenNHibernate.CAD.Ebay
{
public partial class PujaCAD : BasicCAD, IPujaCAD
{
public PujaCAD() : base ()
{
}

public PujaCAD(ISession sessionAux) : base (sessionAux)
{
}



public PujaEN ReadOIDDefault (int id_puja)
{
        PujaEN pujaEN = null;

        try
        {
                SessionInitializeTransaction ();
                pujaEN = (PujaEN)session.Get (typeof(PujaEN), id_puja);
                SessionCommit ();
        }

        catch (Exception ex) {
                SessionRollBack ();
                if (ex is NuevoGenNHibernate.Exceptions.ModelException)
                        throw ex;
                throw new NuevoGenNHibernate.Exceptions.DataLayerException ("Error in PujaCAD.", ex);
        }


        finally
        {
                SessionClose ();
        }

        return pujaEN;
}


public int CrearPuja (PujaEN puja)
{
        try
        {
                SessionInitializeTransaction ();
                if (puja.Pujador != null) {
                        puja.Pujador = (NuevoGenNHibernate.EN.Ebay.UsuarioEN)session.Load (typeof(NuevoGenNHibernate.EN.Ebay.UsuarioEN), puja.Pujador.Nick);

                        puja.Pujador.Pujas.Add (puja);
                }
                if (puja.Subasta != null) {
                        puja.Subasta = (NuevoGenNHibernate.EN.Ebay.SubastaEN)session.Load (typeof(NuevoGenNHibernate.EN.Ebay.SubastaEN), puja.Subasta.Id_venta);

                        puja.Subasta.Pujas.Add (puja);
                }

                session.Save (puja);
                SessionCommit ();
        }

        catch (Exception ex) {
                SessionRollBack ();
                if (ex is NuevoGenNHibernate.Exceptions.ModelException)
                        throw ex;
                throw new NuevoGenNHibernate.Exceptions.DataLayerException ("Error in PujaCAD.", ex);
        }


        finally
        {
                SessionClose ();
        }

        return puja.Id_puja;
}

public void EliminarPuja (int id_puja)
{
        try
        {
                SessionInitializeTransaction ();
                PujaEN pujaEN = (PujaEN)session.Load (typeof(PujaEN), id_puja);
                session.Delete (pujaEN);
                SessionCommit ();
        }

        catch (Exception ex) {
                SessionRollBack ();
                if (ex is NuevoGenNHibernate.Exceptions.ModelException)
                        throw ex;
                throw new NuevoGenNHibernate.Exceptions.DataLayerException ("Error in PujaCAD.", ex);
        }


        finally
        {
                SessionClose ();
        }
}

public PujaEN ReadOID (int id_puja)
{
        PujaEN pujaEN = null;

        try
        {
                SessionInitializeTransaction ();
                pujaEN = (PujaEN)session.Get (typeof(PujaEN), id_puja);
                SessionCommit ();
        }

        catch (Exception ex) {
                SessionRollBack ();
                if (ex is NuevoGenNHibernate.Exceptions.ModelException)
                        throw ex;
                throw new NuevoGenNHibernate.Exceptions.DataLayerException ("Error in PujaCAD.", ex);
        }


        finally
        {
                SessionClose ();
        }

        return pujaEN;
}

public System.Collections.Generic.IList<PujaEN> ReadAll (int first, int size)
{
        System.Collections.Generic.IList<PujaEN> result = null;
        try
        {
                SessionInitializeTransaction ();
                if (size > 0)
                        result = session.CreateCriteria (typeof(PujaEN)).
                                 SetFirstResult (first).SetMaxResults (size).List<PujaEN>();
                else
                        result = session.CreateCriteria (typeof(PujaEN)).List<PujaEN>();
                SessionCommit ();
        }

        catch (Exception ex) {
                SessionRollBack ();
                if (ex is NuevoGenNHibernate.Exceptions.ModelException)
                        throw ex;
                throw new NuevoGenNHibernate.Exceptions.DataLayerException ("Error in PujaCAD.", ex);
        }


        finally
        {
                SessionClose ();
        }

        return result;
}
}
}
